﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace EPiBoost.Core.Helpers
{
	public static class ResponseHelper
	{
		public static string GetResponseFromUrl(string url)
		{
			var async = HttpWebRequest.Create(url);
			var stream = async.GetResponse().GetResponseStream();
			StreamReader reader = new StreamReader(stream);
			return reader.ReadToEnd();
		}


		/// <summary>
		/// Transfers execution to the supplied url.
		/// </summary>
		public class TransferResult : ActionResult
		{
			public string Url { get; private set; }

			public TransferResult(string url)
			{
				this.Url = url;
			}

			public override void ExecuteResult(ControllerContext context)
			{
				//if (context == null)
				//    throw new ArgumentNullException("context");

				var httpContext = HttpContext.Current;

				// MVC 3 running on IIS 7+
				if (HttpRuntime.UsingIntegratedPipeline)
				{
					httpContext.Server.TransferRequest(this.Url);
				}
				else
				{
					// Pre MVC 3
					httpContext.RewritePath(this.Url, false);

					IHttpHandler httpHandler = new MvcHttpHandler();
					httpHandler.ProcessRequest(httpContext);
				}
			}
		}

	}
}
